package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Goods;
import cn.wolfcode.trip.base.mapper.GoodsMapper;
import cn.wolfcode.trip.base.query.GoodsQueryObject;
import cn.wolfcode.trip.base.service.IGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lyu
 */
@Service
public class GoodsServiceImpl implements IGoodsService {

    private final GoodsMapper goodsMapper;

    @Autowired
    public GoodsServiceImpl(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    /**
     * 分页查询商品
     *
     * @param qo 条件
     * @return 商品列表
     */
    @Override
    public PageInfo<Goods> queryForList(GoodsQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo<>(goodsMapper.selectForList(qo));
    }

    /**
     * 新增或保存商品
     *
     * @param goods 商品对象
     */
    @Override
    public void saveOrUpdate(Goods goods) {
        if (goods.getId() == null) {
            goodsMapper.insert(goods);
        } else {
            goodsMapper.updateByPrimaryKey(goods);
        }
    }

    /**
     * 删除商品
     *
     * @param id 商品id
     */
    @Override
    public void delete(Long id) {
        goodsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询所有商品
     *
     * @return 商品
     */
    @Override
    public List<Goods> listAll() {
        return goodsMapper.selectAll();
    }

    /**
     * 查询需要积分最高的商品
     *
     * @return 积分最高的商品
     */
    @Override
    public Goods getExpensive() {
        return goodsMapper.getExpensive();
    }

    /**
     * 根据id查询商品
     *
     * @param id 商品id
     * @return 商品
     */
    @Override
    public Goods get(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }
}
